package com.youzu.chuang;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class MainScreen extends JFrame {

	private static final long serialVersionUID = 1L;

	private JTextPane logTextPanel = new JTextPane();
	
	private JScrollPane scroll = new JScrollPane(logTextPanel);
	
	private JButton oneKeyPublishButton = new JButton("一键发布"); 

	public MainScreen() {
		setLayout(new FlowLayout());
		JPanel clientJPanel = new JPanel();
		clientJPanel.setPreferredSize(new Dimension(400, 50));
		oneKeyPublishButton.setPreferredSize(new Dimension(100, 40));
		logTextPanel.setPreferredSize(new Dimension(900, 400));
		
		JPanel logJPanel = new JPanel();
		logTextPanel.setText("idos");
		
		scroll.setVerticalScrollBarPolicy( 
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		clientJPanel.add(oneKeyPublishButton);
		logJPanel.add(scroll);
		add(logJPanel);
		add(clientJPanel);
//		add(logTextArea);
		
		registerEvent();
		
	}
	
	public void registerEvent() {
		oneKeyPublishButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
//				setDocs("aaaaaaaaaaaaaaaaaaaa", Color.RED, false, 12);
				new Thread(new Runnable() {

					@Override
					public void run() {
						for (int i = 0; i< 100000; i++) {
							setDocs("aaaaaaaaaaaaaaaaaaaa" + i, Color.RED, false, 12);
							
//							logTextPanel.setCaretPosition(logTextPanel.getDocument().getLength());
							
						}
						
					}
					
				}).start();
				
//				
			}
		});
	}
	
	public static void run(final JFrame f, String title, final int width, final int height) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				f.setTitle(title);
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.setBounds(100, 100, width, height);
				f.setVisible(true);
				f.setResizable(false);
			}
			
		});
	}
	
	public void clearSetDefaultString(String defaultString) {
		logTextPanel.setText(defaultString);
	}
	
	public void insert(String str, AttributeSet attrSet) {
		Document doc = logTextPanel.getDocument();
		str = "\n" + str;
		try {
			doc.insertString(doc.getLength(), str, attrSet);
//			logTextPanel.select(doc.getLength(), doc.getLength()); 
//			logTextPanel.setCaretPosition(logTextPanel.getText().length());
			JScrollBar sBar = scroll.getVerticalScrollBar();
			sBar.setValue(logTextPanel.getText().length());
		} catch (BadLocationException e) {
			System.out.println("BadLocationException:   " + e);
		}
	}

	public void setDocs(String str, Color color, boolean bold, int fontSize) {
		SimpleAttributeSet attrSet = new SimpleAttributeSet();
		StyleConstants.setForeground(attrSet, color); // 颜色
		if (bold == true) {
			StyleConstants.setBold(attrSet, true); // 字体类型
		} 
		StyleConstants.setFontSize(attrSet, fontSize); // 字体大小
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				insert(str, attrSet);
			}
		});
		
	}
	
	public static void main(String[] arg0) {
		run(new MainScreen(), "test", 1000, 600);
	}
	
}